import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_syt/api/api.dart';
import 'package:flutter_syt/datas/number_extension.dart';
import 'package:flutter_syt/datas/station_item.dart';
import 'package:flutter_syt/utils/click_utils.dart';
import 'package:flutter_syt/widgets/normal_table.dart';
import 'package:yyy_route_annotation/yyy_route_annotation.dart';

class MessageSubList extends StatefulWidget {
  final StationItem _entity;

  MessageSubList(this._entity);

  @override
  State<StatefulWidget> createState() {
    return _MessageSubListState();
  }
}

class _MessageSubListState extends State<MessageSubList> {
  Widget _item(StationSubItem item) {
    return Container(
      padding: EdgeInsets.only(left: 10, right: 10, top: 10),
      child: Container(
        padding: EdgeInsets.all(15),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(12)),
        ),
        child: Column(
          children: [
            Row(
              children: [
                Text(
                  DateUtil.formatDateMs(parseInt(item.created_at) * 1000),
                  style: TextStyle(
                    color: Color(0xff999999),
                    fontSize: 12,
                    fontWeight: FontWeight.w400,
                  ),
                ),
              ],
            ),
            Padding(padding: EdgeInsets.only(top: 7)),
            Row(
              children: [
                Flexible(
                  child: Text(
                    item.account_name,
                    style: TextStyle(
                      color: Color(0xff000000).withOpacity(0.8),
                      fontSize: 16,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                )
              ],
            ),
            Padding(padding: EdgeInsets.only(top: 7)),
            Row(
              children: [
                Flexible(
                  child: Text(
                    item.content,
                    style: TextStyle(
                      color: Color(0xff000000).withOpacity(0.6),
                      fontSize: 12,
                      fontWeight: FontWeight.w300,
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  String _titile = '';

  @override
  void dispose() {
    super.dispose();
  }

  @override
  void initState() {
    super.initState();
    if (widget._entity.account_name.length > 0) {
      _titile = widget._entity.account_name == 'null'
          ? ''
          : widget._entity.account_name;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(_titile),
      ),
      body: NormalTableView<StationSubItem>(
        itemBuilder: (ctx, item) {
          return GestureDetector(
            onTap: ClickUtils.debounce(() {
              item.is_read = 2;
              RouteManager.push(context, item.url);
            }),
            child: _item(item),
          );
        },
        getData: (page) async {
          var list = await API.shared.station
              .subList(widget._entity.pushlish_user_id, page);
          if (widget._entity.account_name.isEmpty) {
            widget._entity.account_name = list.first.account_name;
            setState(() {});
          }
          if ((_titile.isEmpty && list.length > 0) || _titile == 'null') {
            _titile = list.first.account_name;
            if (mounted) {
              setState(() {});
            }
          }
          return list;
        },
      ),
    );
  }
}
